from flask import Blueprint, request, jsonify
from app.services import ContentService
bp = Blueprint('content', __name__)

@bp.route('', methods=['POST'])
def create_content():
    """创建学习内容"""
    data = request.get_json()
    result = ContentService.create_content(
        creator_id=data.get('creator_id'),
        content_type=data.get('type'),
        title=data.get('title'),
        text=data.get('text'),
        difficulty=data.get('difficulty'),
        pinyin=data.get('pinyin'),
        translation=data.get('translation')
    )
    return jsonify(result), result['code']

@bp.route('/search', methods=['GET'])
def search_contents():
    """搜索学习内容"""
    keyword = request.args.get('q', '')
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 10, type=int)
    result = ContentService.search_contents(
        keyword=keyword,
        page=page,
        per_page=per_page
    )
    return jsonify(result), result['code']

@bp.route('', methods=['GET'])
def get_contents():
    print("get_contents")
    """分页查询学习内容"""
    page = request.args.get('page', 1, type=int)  # 当前页码，默认为 1
    per_page = request.args.get('per_page', 10, type=int)  # 每页条数，默认为 10

    # 调用服务层方法
    result = ContentService.get_contents(page=page, per_page=per_page)
    return jsonify(result), result['code']

@bp.route('/study_management', methods=['GET'])
def get_contents_study_management():
    print("get_contents")
    """分页查询学习内容"""
    page = request.args.get('page', 1, type=int)  # 当前页码，默认为 1
    per_page = request.args.get('per_page', 10, type=int)  # 每页条数，默认为 10
    student_id = request.args.get('student_id', type=int)

    # 调用服务层方法
    result = ContentService.get_contents_study(student_id=student_id,page=page, per_page=per_page)
    return jsonify(result), result['code']


# 删除学习内容
@bp.route('/<int:content_id>', methods=['DELETE','OPTIONS'])
def delete_content(content_id):
    """删除学习内容"""
    result = ContentService.delete_content(content_id)
    return jsonify(result), 200

# 批量删除
@bp.route('', methods=['DELETE','OPTIONS'])
def delete_contents():
    """批量删除学习内容"""
    content_ids = request.get_json().get('content_ids', [])
    result = ContentService.delete_contents(content_ids)
    return jsonify(result), result['code']

@bp.route('/get_content_by_id/<int:content_id>', methods=['GET'])
def get_content_by_id(content_id):
    
    """根据id获取学习内容"""
    result = ContentService.get_content_by_id(content_id)

    return jsonify(result), 200


@bp.route('/update_content/<content_id>', methods=['PUT'])
def update_content(content_id):
    """通用保存接口：content_id为0表示新增，非0表示更新"""
    try:
        # 将字符串"null"或"0"转换为None表示新增
        content_id = None if content_id == 'null' or content_id == '0' else int(content_id)
        
        result = ContentService.update_content(
            content_id=content_id,
            **request.get_json()
        )
        return jsonify(result), result['code']
    except ValueError:
        return jsonify({'code': 400, 'msg': '无效的content_id'}), 400

@bp.route('/tasks/generate', methods=['POST'])
def generate_tasks():
    """生成学习任务"""
    data = request.get_json()
    result = ContentService.generate_tasks(
        student_ids=data.get('student_ids'),
        content_ids=data.get('content_ids'),
    )
    return jsonify(result), result['code']